package com.ruoyi.basedata.task;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.basebill.task.PushEASBillDataTask;
import com.ruoyi.basedata.domain.*;
import com.ruoyi.basedata.mapper.task.OmsBaseDataTaskUtilMapper;
import com.ruoyi.basedata.service.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.service.ISysJobLogService;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.utils.HubUtil;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


/**
 * 定时同步基础资料
 */
@Component("OMSBaseDataTask")
public class OMSBaseDataTask {

    private final static Logger logger = LoggerFactory.getLogger(OMSBaseDataTask.class);

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IBdMaterialService bdMaterialService;

    @Autowired
    private IBdCustomerService bdCustomerService;

    @Autowired
    private IBdSupplierService bdSupplierService;

    @Autowired
    private IBdWarehouseService bdWarehouseService;

    @Autowired
    private OmsBaseDataTaskUtilMapper omsBaseDataTaskUtilMapper;

    @Autowired
    private IBdPurpriceService bdPurpriceService;

    @Autowired
    private IBdCheckoutpriceService bdCheckoutpriceService;


    /**
     * 获取token信息
     */
    private JSONObject loginParam(){
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("loginurl");
        String s = HubUtil.doGetWithJson("" + sysConfig.getConfigValue() + sysConfig.getRemark());
        return JSON.parseObject(s);
    }
    /**
     * 定时同步物料
     */
    public void SynMaterial(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String expires_in = loginParam.get("expires_in").toString();
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("materialUrl");
        String s1 = "";
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynMaterial");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步物料时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++){
            JSONObject jsonObject2 = data.getJSONObject(i);
            BdMaterial bdMaterial = new BdMaterial();
            bdMaterial.setMaterialnumber(jsonObject2.getString("materialnumber"));
            bdMaterial.setMaterialname(jsonObject2.getString("materialname"));
            bdMaterial.setBaseunitnumber(jsonObject2.getString("baseunitnumber"));
            bdMaterial.setBaseunitname(jsonObject2.getString("baseunitname"));
            bdMaterial.setGroup1number(jsonObject2.getString("group1number"));
            bdMaterial.setGroup1name(jsonObject2.getString("group1name"));
            bdMaterial.setGroup2number(jsonObject2.getString("group2number"));
            bdMaterial.setGroup2name(jsonObject2.getString("group2name"));
            bdMaterial.setGroup3number(jsonObject2.getString("group3number"));
            bdMaterial.setGroup3name(jsonObject2.getString("group3name"));
            bdMaterial.setGroup4number(jsonObject2.getString("group4number"));
            bdMaterial.setGroup4name(jsonObject2.getString("group4name"));
            bdMaterial.setTaxrate(jsonObject2.getBigDecimal("tax_rate"));
            bdMaterial.setCreatetime(jsonObject2.getDate("createtime"));
            bdMaterial.setUpdatetime(jsonObject2.getDate("write_date"));
            BdMaterial tempMaterial = new BdMaterial();
            tempMaterial.setMaterialnumber(bdMaterial.getMaterialnumber());
            List<BdMaterial> bdMaterials = bdMaterialService.selectBdMaterialList(tempMaterial);
            if (StringUtils.isEmpty(bdMaterials)){
                bdMaterialService.insertBdMaterial(bdMaterial);
            }else {
                if(bdMaterials.size()!=1){
                    for(int j = 1;j<bdMaterials.size();j++){
                        bdMaterialService.deleteBdMaterialById(bdMaterials.get(j).getId());
                    }
                }
                bdMaterial.setId(bdMaterials.get(0).getId());
                bdMaterialService.updateBdMaterial(bdMaterial);
            }

        }
    }
    /**
     * 同步所有物料
     */
    public void SynAllMaterial(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("materialUrl");
        String s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        logger.info("--------------全量同步物料时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++){
            JSONObject jsonObject2 = data.getJSONObject(i);
            BdMaterial bdMaterial = new BdMaterial();
            bdMaterial.setMaterialnumber(jsonObject2.getString("materialnumber"));
            bdMaterial.setMaterialname(jsonObject2.getString("materialname"));
            bdMaterial.setBaseunitnumber(jsonObject2.getString("baseunitnumber"));
            bdMaterial.setBaseunitname(jsonObject2.getString("baseunitname"));
            bdMaterial.setGroup1number(jsonObject2.getString("group1number"));
            bdMaterial.setGroup1name(jsonObject2.getString("group1name"));
            bdMaterial.setGroup2number(jsonObject2.getString("group2number"));
            bdMaterial.setGroup2name(jsonObject2.getString("group2name"));
            bdMaterial.setGroup3number(jsonObject2.getString("group3number"));
            bdMaterial.setGroup3name(jsonObject2.getString("group3name"));
            bdMaterial.setGroup4number(jsonObject2.getString("group4number"));
            bdMaterial.setGroup4name(jsonObject2.getString("group4name"));
            bdMaterial.setTaxrate(jsonObject2.getBigDecimal("tax_rate"));
            bdMaterial.setCreatetime(jsonObject2.getDate("createtime"));
            bdMaterial.setUpdatetime(jsonObject2.getDate("write_date"));
            BdMaterial tempMaterial = new BdMaterial();
            tempMaterial.setMaterialnumber(bdMaterial.getMaterialnumber());
            List<BdMaterial> bdMaterials = bdMaterialService.selectBdMaterialList(tempMaterial);
            if (StringUtils.isEmpty(bdMaterials)){
                bdMaterialService.insertBdMaterial(bdMaterial);
            }else {
                if(bdMaterials.size()!=1){
                    for(int j = 1;j<bdMaterials.size();j++){
                        bdMaterialService.deleteBdMaterialById(bdMaterials.get(j).getId());
                    }
                }
                bdMaterial.setId(bdMaterials.get(0).getId());
                bdMaterialService.updateBdMaterial(bdMaterial);
            }

        }
    }
    /**
     * 定时同步仓库
     */
    public void SynWarehouse(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String expires_in = loginParam.get("expires_in").toString();
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("warehouseUrl");
        String s1 = "";
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynWarehouse");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步仓库时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(jsonObject1.getString("warehousenumber"));
            bdWarehouse.setWarehousename(jsonObject1.getString("warehousename"));
            bdWarehouse.setStorageorgnumber(jsonObject1.getString("storageorgnumber"));
            bdWarehouse.setStorageorgname(jsonObject1.getString("storageorgname"));
            bdWarehouse.setTransferwhnumber(jsonObject1.getString("transferwhnumber"));
            bdWarehouse.setTransferwhname(jsonObject1.getString("transferwhname"));
            bdWarehouse.setCreatetime(jsonObject1.getDate("createtime"));
            bdWarehouse.setUpdatetime(jsonObject1.getDate("write_date"));
            BdWarehouse tempWareHouse = new BdWarehouse();
            tempWareHouse.setWarehousenumber(bdWarehouse.getWarehousenumber());
            List<BdWarehouse> tempWareHouses = bdWarehouseService.selectBdWarehouseList(tempWareHouse);
            if (StringUtils.isEmpty(tempWareHouses)){
                bdWarehouseService.insertBdWarehouse(bdWarehouse);
            }else {
                if(tempWareHouses.size()!=1){
                    for(int j = 1;j<tempWareHouses.size();j++){
                        bdWarehouseService.deleteBdWarehouseById(tempWareHouses.get(j).getId());
                    }
                }
                bdWarehouse.setId(tempWareHouses.get(0).getId());
                bdWarehouseService.updateBdWarehouse(bdWarehouse);
            }

        }
    }
    /**
     * 同步所有仓库
     */
    public void SynAllWarehouse(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("warehouseUrl");
        String s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        logger.info("--------------全量同步仓库时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(jsonObject1.getString("warehousenumber"));
            bdWarehouse.setWarehousename(jsonObject1.getString("warehousename"));
            bdWarehouse.setStorageorgnumber(jsonObject1.getString("storageorgnumber"));
            bdWarehouse.setStorageorgname(jsonObject1.getString("storageorgname"));
            bdWarehouse.setTransferwhnumber(jsonObject1.getString("transferwhnumber"));
            bdWarehouse.setTransferwhname(jsonObject1.getString("transferwhname"));
            bdWarehouse.setCreatetime(jsonObject1.getDate("createtime"));
            bdWarehouse.setUpdatetime(jsonObject1.getDate("write_date"));
            BdWarehouse tempWareHouse = new BdWarehouse();
            tempWareHouse.setWarehousenumber(bdWarehouse.getWarehousenumber());
            List<BdWarehouse> tempWareHouses = bdWarehouseService.selectBdWarehouseList(tempWareHouse);
            if (StringUtils.isEmpty(tempWareHouses)){
                bdWarehouseService.insertBdWarehouse(bdWarehouse);
            }else {
                if(tempWareHouses.size()!=1){
                    for(int j = 1;j<tempWareHouses.size();j++){
                        bdWarehouseService.deleteBdWarehouseById(tempWareHouses.get(j).getId());
                    }
                }
                bdWarehouse.setId(tempWareHouses.get(0).getId());
                bdWarehouseService.updateBdWarehouse(bdWarehouse);
            }

        }
    }
    /**
     * 定时同步客户
     */
    public void SynCustomer(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String expires_in = loginParam.get("expires_in").toString();
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("customerUrl");
        String s1 = "";
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynCustomer");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步客户时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdCustomer bdCustomer = new BdCustomer();
            bdCustomer.setCustomernumber(jsonObject1.getString("customernumber"));
            bdCustomer.setCustomername(jsonObject1.getString("customername"));
            bdCustomer.setGroupnumber(jsonObject1.getString("groupnumber"));
            bdCustomer.setGroupname(jsonObject1.getString("groupname"));
            bdCustomer.setIsinternal("true".equals(jsonObject1.getString("isinternal"))?1L:0L);
            bdCustomer.setInternalcompanynumber(jsonObject1.getString("internalcompanynumber"));
            bdCustomer.setInternalcompanyname(jsonObject1.getString("internalcompanyname"));
            bdCustomer.setIscostcenter("true".equals(jsonObject1.getString("iscostcenter"))?1L:0L);
            bdCustomer.setCostcenternumber(jsonObject1.getString("costcenternumber"));
            bdCustomer.setCostcentername(jsonObject1.getString("costcentername"));
            bdCustomer.setSaleorgnumber(jsonObject1.getString("saleorgnumber"));
            bdCustomer.setSaleorgname(jsonObject1.getString("saleorgname"));
            bdCustomer.setAccountcompanynumber(jsonObject1.getString("accountcompanynumber"));
            bdCustomer.setAccountcompanyname(jsonObject1.getString("accountcompanyname"));
            bdCustomer.setStorewhnumber(jsonObject1.getString("storewhnumber"));
            bdCustomer.setStorewhname(jsonObject1.getString("storewhname"));
            bdCustomer.setVouchertypenumber(jsonObject1.getString("vouchertypenumber"));
            bdCustomer.setVouchertypename(jsonObject1.getString("vouchertypename"));
            bdCustomer.setCreatetime(jsonObject1.getDate("createtime"));
            bdCustomer.setUpdatetime(jsonObject1.getDate("write_date"));
            BdCustomer tempCustomer = new BdCustomer();
            tempCustomer.setCustomernumber(bdCustomer.getCustomernumber());
            List<BdCustomer> tempCustomers = bdCustomerService.selectBdCustomerList(tempCustomer);
            if (StringUtils.isEmpty(tempCustomers)){
                bdCustomerService.insertBdCustomer(bdCustomer);
            }else {
                if(tempCustomers.size()!=1){
                    for(int j = 1;j<tempCustomers.size();j++){
                        bdCustomerService.deleteBdCustomerById(tempCustomers.get(j).getId());
                    }
                }
                bdCustomer.setId(tempCustomers.get(0).getId());
                bdCustomerService.updateBdCustomer(bdCustomer);
            }

        }
    }
    /**
     * 同步所有客户
     */
    public void SynAllCustomer(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("customerUrl");
        String s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        logger.info("--------------全量同步客户时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdCustomer bdCustomer = new BdCustomer();
            bdCustomer.setCustomernumber(jsonObject1.getString("customernumber"));
            bdCustomer.setCustomername(jsonObject1.getString("customername"));
            bdCustomer.setGroupnumber(jsonObject1.getString("groupnumber"));
            bdCustomer.setGroupname(jsonObject1.getString("groupname"));
            bdCustomer.setIsinternal("true".equals(jsonObject1.getString("isinternal"))?1L:0L);
            bdCustomer.setInternalcompanynumber(jsonObject1.getString("internalcompanynumber"));
            bdCustomer.setInternalcompanyname(jsonObject1.getString("internalcompanyname"));
            bdCustomer.setIscostcenter("true".equals(jsonObject1.getString("iscostcenter"))?1L:0L);
            bdCustomer.setCostcenternumber(jsonObject1.getString("costcenternumber"));
            bdCustomer.setCostcentername(jsonObject1.getString("costcentername"));
            bdCustomer.setSaleorgnumber(jsonObject1.getString("saleorgnumber"));
            bdCustomer.setSaleorgname(jsonObject1.getString("saleorgname"));
            bdCustomer.setAccountcompanynumber(jsonObject1.getString("accountcompanynumber"));
            bdCustomer.setAccountcompanyname(jsonObject1.getString("accountcompanyname"));
            bdCustomer.setStorewhnumber(jsonObject1.getString("storewhnumber"));
            bdCustomer.setStorewhname(jsonObject1.getString("storewhname"));
            bdCustomer.setVouchertypenumber(jsonObject1.getString("vouchertypenumber"));
            bdCustomer.setVouchertypename(jsonObject1.getString("vouchertypename"));
            bdCustomer.setCreatetime(jsonObject1.getDate("createtime"));
            bdCustomer.setUpdatetime(jsonObject1.getDate("write_date"));
            BdCustomer tempCustomer = new BdCustomer();
            tempCustomer.setCustomernumber(bdCustomer.getCustomernumber());
            List<BdCustomer> tempCustomers = bdCustomerService.selectBdCustomerList(tempCustomer);
            if (StringUtils.isEmpty(tempCustomers)){
                bdCustomerService.insertBdCustomer(bdCustomer);
            }else {
                if(tempCustomers.size()!=1){
                    for(int j = 1;j<tempCustomers.size();j++){
                        bdCustomerService.deleteBdCustomerById(tempCustomers.get(j).getId());
                    }
                }
                bdCustomer.setId(tempCustomers.get(0).getId());
                bdCustomerService.updateBdCustomer(bdCustomer);
            }

        }
    }
    /**
     * 定时同步供应商
     */
    public void SynSupplier(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String expires_in = loginParam.get("expires_in").toString();
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("supplierUrl");
        String s1 = "";
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynSupplier");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步供应商时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdSupplier bdSupplier = new BdSupplier();
            bdSupplier.setSuppliernumber(jsonObject1.getString("suppliernumber"));
            bdSupplier.setSuppliername(jsonObject1.getString("suppliername"));
            bdSupplier.setGroupnumber(jsonObject1.getString("groupnumber"));
            bdSupplier.setGroupname(jsonObject1.getString("groupname"));
            bdSupplier.setIsinternal("true".equals(jsonObject1.getString("isinternal"))?1L:0L);
            bdSupplier.setInternalcompanynumber(jsonObject1.getString("internalcompanynumber"));
            bdSupplier.setInternalcompanyname(jsonObject1.getString("internalcompanyname"));
            bdSupplier.setCreatetime(jsonObject1.getDate("createtime"));
            bdSupplier.setUpdatetime(jsonObject1.getDate("write_date"));
            BdSupplier tempSupplier = new BdSupplier();
            tempSupplier.setSuppliernumber(bdSupplier.getSuppliernumber());
            List<BdSupplier> tempSuppliers = bdSupplierService.selectBdSupplierList(tempSupplier);
            if (StringUtils.isEmpty(tempSuppliers)){
                bdSupplierService.insertBdSupplier(bdSupplier);
            }else {
                if(tempSuppliers.size()!=1){
                    for(int j = 1;j<tempSuppliers.size();j++){
                        bdSupplierService.deleteBdSupplierById(tempSuppliers.get(j).getId());
                    }
                }
                bdSupplier.setId(tempSuppliers.get(0).getId());
                bdSupplierService.updateBdSupplier(bdSupplier);
            }
        }
    }
    /**
     *同步所有供应商
     */
    public void SynAllSupplier(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("supplierUrl");
        String s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        logger.info("--------------全量同步供应商时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdSupplier bdSupplier = new BdSupplier();
            bdSupplier.setSuppliernumber(jsonObject1.getString("suppliernumber"));
            bdSupplier.setSuppliername(jsonObject1.getString("suppliername"));
            bdSupplier.setGroupnumber(jsonObject1.getString("groupnumber"));
            bdSupplier.setGroupname(jsonObject1.getString("groupname"));
            bdSupplier.setIsinternal("true".equals(jsonObject1.getString("isinternal"))?1L:0L);
            bdSupplier.setInternalcompanynumber(jsonObject1.getString("internalcompanynumber"));
            bdSupplier.setInternalcompanyname(jsonObject1.getString("internalcompanyname"));
            bdSupplier.setCreatetime(jsonObject1.getDate("createtime"));
            bdSupplier.setUpdatetime(jsonObject1.getDate("write_date"));
            BdSupplier tempSupplier = new BdSupplier();
            tempSupplier.setSuppliernumber(bdSupplier.getSuppliernumber());
            List<BdSupplier> tempSuppliers = bdSupplierService.selectBdSupplierList(tempSupplier);
            if (StringUtils.isEmpty(tempSuppliers)){
                bdSupplierService.insertBdSupplier(bdSupplier);
            }else {
                if(tempSuppliers.size()!=1){
                    for(int j = 1;j<tempSuppliers.size();j++){
                        bdSupplierService.deleteBdSupplierById(tempSuppliers.get(j).getId());
                    }
                }
                bdSupplier.setId(tempSuppliers.get(0).getId());
                bdSupplierService.updateBdSupplier(bdSupplier);
            }
        }
    }
    /**
     * 定时同步考核价
     */
    public void SynCheckoutprice(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("checkoutpriceUrl");
        String s1 = "";
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynCheckoutprice");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步考核价时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        Date date = new Date();
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdCheckoutprice bdCheckoutprice = new BdCheckoutprice();
            bdCheckoutprice.setTypenumber(jsonObject1.getString("groupNumber"));
            bdCheckoutprice.setKingdeeid(jsonObject1.getString("FID"));
            bdCheckoutprice.setTypename(jsonObject1.getString("groupName"));
            bdCheckoutprice.setPrice(jsonObject1.getBigDecimal("price"));
            bdCheckoutprice.setPriority(jsonObject1.getLong("priority"));
            bdCheckoutprice.setEffectivedate(jsonObject1.getDate("effectiveDate"));
            bdCheckoutprice.setExpiredate(jsonObject1.getDate("expireDate"));
            bdCheckoutprice.setMaterialnumber(jsonObject1.getString("materialNumber"));
            bdCheckoutprice.setMaterialname(jsonObject1.getString("materialName"));
            bdCheckoutprice.setUnitnumber(jsonObject1.getString("baseUnitNumber"));
            bdCheckoutprice.setUnitname(jsonObject1.getString("baseUnitName"));

            BdCheckoutprice tempCheckoutprice = new BdCheckoutprice();
            tempCheckoutprice.setKingdeeid(jsonObject1.getString("FID"));
            tempCheckoutprice.setStatus(1L);
            List<BdCheckoutprice> tempCheckoutprices = bdCheckoutpriceService.selectBdCheckoutpriceList(tempCheckoutprice);
            if (StringUtils.isNotEmpty(tempCheckoutprices)){
                BdCheckoutprice bdCheckoutprice1 = tempCheckoutprices.get(0);
                bdCheckoutprice1.setStatus(0L);
                bdCheckoutprice1.setStopdate(date);
                bdCheckoutpriceService.updateBdCheckoutprice(bdCheckoutprice1);
            }
            bdCheckoutpriceService.insertBdCheckoutprice(bdCheckoutprice);
        }
    }

    /**
     * 考核价全量同步
     */
    public void SynCheckoutpriceAll(Integer days){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("checkoutpriceUrl");
        //同步之前将所有的状态为正常的数据旧数据置为废弃
        bdCheckoutpriceService.banAllCheckoutprice();
        //根据当前系统时间，回退60天，再根据这个时间，删除之前的数据
        LocalDate localDate = LocalDate.now().minusDays(days);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        bdCheckoutpriceService.del60DaysData(localDate.format(dateTimeFormatter));
        String s1 = "";
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        logger.info("--------------全量同步考核价时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        Date date = new Date();
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdCheckoutprice bdCheckoutprice = new BdCheckoutprice();
            bdCheckoutprice.setTypenumber(jsonObject1.getString("groupNumber"));
            bdCheckoutprice.setKingdeeid(jsonObject1.getString("FID"));
            bdCheckoutprice.setTypename(jsonObject1.getString("groupName"));
            bdCheckoutprice.setPrice(jsonObject1.getBigDecimal("price"));
            bdCheckoutprice.setPriority(jsonObject1.getLong("priority"));
            bdCheckoutprice.setEffectivedate(jsonObject1.getDate("effectiveDate"));
            bdCheckoutprice.setExpiredate(jsonObject1.getDate("expireDate"));
            bdCheckoutprice.setMaterialnumber(jsonObject1.getString("materialNumber"));
            bdCheckoutprice.setMaterialname(jsonObject1.getString("materialName"));
            bdCheckoutprice.setUnitnumber(jsonObject1.getString("baseUnitNumber"));
            bdCheckoutprice.setUnitname(jsonObject1.getString("baseUnitName"));

//            BdCheckoutprice tempCheckoutprice = new BdCheckoutprice();
//            tempCheckoutprice.setKingdeeid(jsonObject1.getString("FID"));
//            tempCheckoutprice.setStatus(1L);
//            List<BdCheckoutprice> tempCheckoutprices = bdCheckoutpriceService.selectBdCheckoutpriceList(tempCheckoutprice);
//            if (StringUtils.isNotEmpty(tempCheckoutprices)){
//                BdCheckoutprice bdCheckoutprice1 = tempCheckoutprices.get(0);
//                bdCheckoutprice1.setStatus(0L);
//                bdCheckoutprice1.setStopdate(date);
//                bdCheckoutpriceService.updateBdCheckoutprice(bdCheckoutprice1);
//            }
            bdCheckoutpriceService.insertBdCheckoutprice(bdCheckoutprice);
        }
    }
    /**
     * 定时同步采购价
     */
    public void SynPurprice(){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String s1 = "";
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("purpriceUrl");
        Date successData = omsBaseDataTaskUtilMapper.getSuccessData("OMSBaseDataTask.SynPurprice");
        //判断该定时任务之前是否存在成功的调度日志，有的话获取时间
        if(!StringUtils.isNull(successData)){
            //增量获取
            String format = new SimpleDateFormat("yyyy-MM-dd").format(successData);
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + format + "'"
                    , token_type, access_token);
        }else{
            //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        }
        logger.info("--------------增量同步采购价时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        Date date = new Date();
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdPurprice bdPurprice = new BdPurprice();
            bdPurprice.setMaterialnumber(jsonObject1.getString("materialNumber"));
            bdPurprice.setMaterialname(jsonObject1.getString("materialName"));
            bdPurprice.setKingdeeid(jsonObject1.getString("FID"));
            bdPurprice.setEffectivedate(jsonObject1.getDate("effectiveDate"));
            bdPurprice.setExpiredate(jsonObject1.getDate("expireDate"));
            bdPurprice.setUnitnumber(jsonObject1.getString("baseUnitNumber"));
            bdPurprice.setUnitname(jsonObject1.getString("baseUnitName"));
            bdPurprice.setPrice(jsonObject1.getBigDecimal("price"));
            bdPurprice.setIstaxprice(jsonObject1.getLong("isTaxPrice"));
            bdPurprice.setTaxrate(jsonObject1.getBigDecimal("taxRate"));

            BdPurprice tempPurprice = new BdPurprice();
            tempPurprice.setKingdeeid(jsonObject1.getString("FID"));
            tempPurprice.setStatus(1L);
            List<BdPurprice> tempPurprices = bdPurpriceService.selectBdPurpriceList(tempPurprice);
            if (StringUtils.isNotEmpty(tempPurprices)){
                BdPurprice bdPurprice1 = tempPurprices.get(0);
                bdPurprice1.setStatus(0L);
                bdPurprice1.setStopdate(date);
                bdPurpriceService.updateBdPurprice(bdPurprice1);
            }
            bdPurpriceService.insertBdPurprice(bdPurprice);
        }
    }


    /**
     * 采购价全量同步
     */
    public void SynPurpriceAll(Integer days){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("purpriceUrl");
        //同步前将所有的正常的数据都置为废弃
        bdPurpriceService.banAllPurprice();
        //根据当前系统时间，回退60天，根据这个时间，删除之前的数据
        LocalDate localDate = LocalDate.now().minusDays(days);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        bdPurpriceService.del60DaysData(localDate.format(dateTimeFormatter));
        String s1 = "";
        //全量获取
            s1 = HubUtil.doPostWithJson(sysConfig.getConfigValue(), token_type, access_token);
        if(StringUtils.isBlank(s1)){
            return;
        }
        logger.info("--------------全量同步采购价时，获取的数据：" + s1);
        if(StringUtils.isBlank(s1)){
            return;
        }
        Date date = new Date();
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        for(int i = 0;i<data.size();i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            BdPurprice bdPurprice = new BdPurprice();
            bdPurprice.setMaterialnumber(jsonObject1.getString("materialNumber"));
            bdPurprice.setMaterialname(jsonObject1.getString("materialName"));
            bdPurprice.setKingdeeid(jsonObject1.getString("FID"));
            bdPurprice.setEffectivedate(jsonObject1.getDate("effectiveDate"));
            bdPurprice.setExpiredate(jsonObject1.getDate("expireDate"));
            bdPurprice.setUnitnumber(jsonObject1.getString("baseUnitNumber"));
            bdPurprice.setUnitname(jsonObject1.getString("baseUnitName"));
            bdPurprice.setPrice(jsonObject1.getBigDecimal("price"));
            bdPurprice.setIstaxprice(jsonObject1.getLong("isTaxPrice"));
            bdPurprice.setTaxrate(jsonObject1.getBigDecimal("taxRate"));

//            BdPurprice tempPurprice = new BdPurprice();
//            tempPurprice.setKingdeeid(jsonObject1.getString("FID"));
//            tempPurprice.setStatus(1L);
//            List<BdPurprice> tempPurprices = bdPurpriceService.selectBdPurpriceList(tempPurprice);
//            if (StringUtils.isNotEmpty(tempPurprices)){
//                BdPurprice bdPurprice1 = tempPurprices.get(0);
//                bdPurprice1.setStatus(0L);
//                bdPurprice1.setStopdate(date);
//                bdPurpriceService.updateBdPurprice(bdPurprice1);
//            }
            bdPurpriceService.insertBdPurprice(bdPurprice);
        }
    }

    /**
     *
     * @param dateString
     */
    public void testPurPrice(String dateString){
        JSONObject loginParam = loginParam();
        String access_token = (String) loginParam.get("access_token");
        String token_type = (String) loginParam.get("token_type");
        String s1 = "";
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("purpriceUrl");
            //增量获取
            s1 = HubUtil.doGetWithJson(
                    sysConfig.getConfigValue() + sysConfig.getRemark() + "'" + dateString + "'"
                    , token_type, access_token);
        if(StringUtils.isBlank(s1)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        JSONArray data = jsonObject.getJSONArray("data");
        System.out.println("111");
    }
}
